App.controller('busCheckController', ['$scope', '$state', '$stateParams', 'Page', function($scope, $state, $stateParams, Page) {
	var date = new Date();
	var now = date.getFullYear() + "-" + ((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : '0' + (date.getMonth() + 1))
		+ "-" + (date.getDate() > 9 ? date.getDate() : '0' + date.getDate()) ;
	$scope.uploadTime = now;

	$scope.$watch('mediaBusGrid.radioModel', function (newValue, oldValue, $scope) {
		if(newValue){
			designColDefs(newValue);
		}
	});
	$scope.mediaBusGrid = {
		adCheck : function(row) {
			$scope.adBusCheckGrid.query.resourceId = row.resourceId;
			$scope.adBusCheckGrid.query();
			$scope.adBusCheckModal.show();
		},
		resouceCheck : function(row) {
			$scope.allTimesMap = new Map();
			$scope.busCheck = row;
			Page.ajaxGetQuiet('mediaCheck/getPublishImgs.do', {dataId : row.resourceId,dataType : '2'}, function (resp) {
				$scope.allImgs = resp.data;
				for (var i = 0; i < $scope.allImgs.length; i++) {
					if($scope.allTimesMap.get($scope.allImgs[i].createTime.substring(0,10)) == undefined) {
						$scope.allTimesMap.put($scope.allImgs[i].createTime.substring(0,10),$scope.allImgs[i].createTime.substring(0,10))
					}
				}
				$scope.checkBusResourceModal.show();
			});
		}
	};
	$scope.$on('$viewContentLoaded', function() {
		Page.ajaxGetQuiet('mediaType/getMediaTypesByParentId.do', {parentId : '1'}, function (resp) {
			$scope.busMediaTypes = resp.data;
			for (var i = 0; i < $scope.busMediaTypes.length; i++) {
				if($scope.busMediaTypes[i].name == "车身") {
					//$scope.mediaBusGrid.query.mediaType = $scope.mediaTypes[i].value;
					$scope.basicMediaType = $scope.busMediaTypes[i].value;
					break;
				}
			}
			designColDefs($scope.basicMediaType);
		});
	});

	$scope.dynamicQuery = function() {
		var queryMediaType;
		if($scope.mediaBusGrid.query.mediaType != undefined &&  $scope.mediaBusGrid.query.mediaType != "") {
			queryMediaType = $scope.mediaBusGrid.query.mediaType;
		} else {
			queryMediaType = $scope.basicMediaType;
		}
		designColDefs(queryMediaType);
	}

	$scope.adBusCheckGrid = {
		options : {
			lazy : true,
			bAutoWidth : false
		},
		column_defs: [
			{
				target : 0,
				template: function(row) {
					return '<span ed-click="view" ed-hover="showAll">' + row.billNo.substring(0,10) + '...' + '</span>';
				}
			},
			{
				target: 8,
				template: function(row) {
					if(row.billImgCount > 0) {
						return '<a ed-click="viewBillImgs">预览</a>';
					}
					return '<span></span>'
				}
			},
			{
				target: 9,
				template: function(row) {
					if(row.makeBillImgCount > 0) {
						return '<a ed-click="viewMakeBillImgs">预览</a>';
					}
					return '<span></span>'
				}
			},
			{
				target: 12,
				buttons: [
					{
						name: '检查',
						class: 'btn-edit',
						icon: 'fa-edit',
						event: 'check'
					}
				]
			}
		],
		showAll : function(row) {
			$scope.showBillNo = row.billNo;
			$scope.billNoTips.show(event);
		},
		showAllLeave : function(row) {
			$scope.billNoTips.hide();
		},
		viewBillImgs : function(row) {
			Page.ajaxGetQuiet('mediaCheck/getPublishImgs.do', {dataId : row.billId,dataType : '4'}, function (resp) {
				$scope.billImgDatas = resp.data;
				$scope.viewBillImgsModal.show();
			});
		},
		viewMakeBillImgs : function(row) {
			Page.ajaxGetQuiet('mediaCheck/getPublishImgs.do', {dataId : row.makeBillId,dataType : '1'}, function (resp) {
				$scope.makeBillImgDatas = resp.data;
				$scope.viewMakeBillImgsModal.show();
			});
		},
		check : function(row) {
			$scope.adCheck = row;
			$scope.adTimesMap = new Map();
			Page.ajaxGetQuiet('mediaCheck/getPublishImgs.do', {dataId : row.makeBillId,resourceId : row.resourceId, dataType : '3'}, function (resp) {
				$scope.hisAdCheckDatas = resp.data;
				for (var i = 0; i < $scope.hisAdCheckDatas.length; i++) {
					if($scope.adTimesMap.get($scope.hisAdCheckDatas[i].createTime.substring(0,10)) == undefined) {
						$scope.adTimesMap.put($scope.hisAdCheckDatas[i].createTime.substring(0,10),$scope.hisAdCheckDatas[i].createTime.substring(0,10))
					}
				}
				$scope.checkBusAdModal.show();
			});
		},
		deleteAdImg : function(file) {
			if($scope.copyAdCheck.files.length > 0) {
				for (var i = 0; i < $scope.copyAdCheck.files.length; i++) {
					if($scope.copyAdCheck.files[i].path == file.path) {
						$scope.copyAdCheck.files.splice(i,1);
						$scope.adCheck.files.splice(i,1);
					}
				}
			}
			if($scope.copyAdCheck.files.length == 0) {
				$scope.copyAdCheck.result = "";
				$scope.copyAdCheck.comment = "";
			}
		}
	}

	function designColDefs(mediaType) {
		Page.ajaxGetQuiet('mediaCheck/dynamicHeadLength.do', {mediaType : mediaType}, function (resp) {
			$scope.headLength = resp.data.headLength;
			var column_defs = [
				{
					target: $scope.headLength + 4,
					buttons: [
						{
							name: '广告检查',
							class: 'btn-edit',
							icon: 'glyphicon glyphicon-pushpin',
							event: 'adCheck'
						},
						{
							name: '资源检查',
							class: 'btn btn-success',
							icon: 'glyphicon glyphicon-pushpin',
							event: 'resouceCheck'
						}
					]
				}
			];
			$scope.mediaBusGrid.column_defs = column_defs;
			$scope.mediaBusGrid.setDynamicTable('mediaCheck/dynamicBusHead.do',{mediaType : mediaType});
		});

	}

	$scope.$watch('busCheck.imgFiles.length',function(newValue,oldValue, scope){
		if(newValue != undefined) {
			setTimeout(function(){
				if($scope.busCheck.imgFiles.length > 0) {
					for (var i = 0; i < $scope.busCheck.imgFiles.length; i++) {
						if($scope.busCheck.imgFiles[i].type != "bmp" && $scope.busCheck.imgFiles[i].type != "gif"
							&& $scope.busCheck.imgFiles[i].type != "png" && $scope.busCheck.imgFiles[i].type != "jpg"
							&& $scope.busCheck.imgFiles[i].type != "jpeg") {
							Page.notice("请上传图片!");
							$scope.copyResCheck = {};
							$scope.busCheck.imgFiles.splice(i,1);
							return;
						}
					}
					if($scope.busCheck.imgFiles.length > 0) {
						$scope.copyResCheck = angular.copy($scope.busCheck);
						$scope.$apply($scope.copyResCheck);
					}
				}
			},200)
		}
	});

	$scope.$watch('adCheck.files.length',function(newValue,oldValue, scope){
		if(newValue != undefined) {
			setTimeout(function(){
				if($scope.adCheck.files.length > 0) {
					for (var i = 0; i < $scope.adCheck.files.length; i++) {
						if($scope.adCheck.files[i].type != "bmp" && $scope.adCheck.files[i].type != "gif"
							&& $scope.adCheck.files[i].type != "png" && $scope.adCheck.files[i].type != "jpg"
							&& $scope.adCheck.files[i].type != "jpeg") {
							Page.notice("请上传图片!");
							$scope.copyAdCheck = {};
							$scope.adCheck.files.splice(i,1);
							return;
						}
					}
				}
				if($scope.adCheck.files.length > 0) {
					$scope.copyAdCheck = angular.copy($scope.adCheck);
					$scope.$apply($scope.copyAdCheck);
				}
			},200)

		}
	});


	$scope.deleteImg = function(value) {
		for (var i = 0; i < $scope.copyResCheck.imgFiles.length; i++) {
			if($scope.copyResCheck.imgFiles[i].path == value.path ) {
				$scope.copyResCheck.imgFiles.splice(i,1);
				$scope.busCheck.imgFiles.splice(i,1);
			}
		}
		if($scope.copyResCheck.imgFiles.length == 0) {
			$scope.copyResCheck.result = "";
			$scope.copyResCheck.comment = "";
		}
	}

	$scope.checkBusResourceModal = {
		onOkClick : function() {
			if($scope.copyResCheck == undefined || $scope.copyResCheck.imgFiles == undefined || $scope.copyResCheck.imgFiles.length == 0) {
				Page.notice("您还未上传图片!");
				return;
			}
			if(!$scope.resourceValidateForm.$valid) {
				$scope.resourceValidateForm.doValidate();
			} else {
				Page.ajaxGetQuiet('mediaCheck/saveResourceImgs.do', {dataId : $scope.copyResCheck.resourceId,
					files : $scope.copyResCheck.imgFiles,result : $scope.copyResCheck.result,
					comment : $scope.copyResCheck.comment,sourceType : '2'}, function (resp) {
					Page.notice("提交成功!");
					$scope.checkBusResourceModal.hide();
				});
			}

		},
		onHidden : function() {
			$scope.busCheck.imgFiles = [];
			$scope.copyResCheck = {};
		}
	}

	$scope.checkBusAdModal = {
		onOkClick : function() {
			if($scope.copyAdCheck == undefined || $scope.copyAdCheck.files == undefined || $scope.copyAdCheck.files.length == 0) {
				Page.notice("您还未上传图片!");
				return;
			}
			if(!$scope.adValidateForm.$valid) {
				$scope.adValidateForm.doValidate();
			} else {
				Page.ajaxGetQuiet('mediaCheck/saveResourceImgs.do', {dataId : $scope.copyAdCheck.makeBillId,
					resourceId : $scope.copyAdCheck.resourceId,publishDetailId : $scope.copyAdCheck.id,
					files : $scope.copyAdCheck.files,result : $scope.copyAdCheck.result,
					comment : $scope.copyAdCheck.comment,sourceType : '3'}, function (resp) {
					Page.notice("提交成功!");
					$scope.checkBusAdModal.hide();
				});
			}

		},
		onHidden : function() {
			$scope.adCheck.files = [];
			$scope.copyAdCheck = {};
		}
	}



}]);


